<?php
/**
 * This file is part of won-uploader.
 *
 * Author: Daniel K.
 *
 * won-uploader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * won-uploader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with won-uploader. If not, see <http://www.gnu.org/licenses/>.
 */
   
 if( !defined("won-uploader")) die();    // Silence is golden
 
 require_once $_paths->getIncludePath("Exceptions");
 
 class LanguagePool
 {
 	private $language;	// Current language
 	private $pool;		// Language pool
 	
 	/**
 	 * Constructs a new object of <code>LanguagePool</code>
 	 * @param string $lang	Language to use
 	 */
 	public function __construct($lang)
 	{
 		$this->language = $lang;
 		$this->pool     = array();
 	}
 	
 	/**
 	 * Initializes the language pool
 	 */
 	public function initialize()
 	{
 		global $_paths;
 		
 		$file = $_paths->getLanguageFile($this->language);
 		
 		if( !file_exists($file) )
 			throw new Exception("language file not found",Exceptions::LANGUAGE_FILE_NOT_FOUND);
 		
 		$string = array();
 		
 		require_once $file;
 		
 		$this->pool = $string;
 	}
 	
 	/**
 	 * Returns a string to the given key.
 	 * @param	string	$key	Key to use
 	 */
 	public function get($key)
 	{
 		return $this->pool[$key];
 	}
 	
 	/**
 	 * Returns the used language code
 	 * @return	string
 	 */
 	public function getLanguage()
 	{
 		return $this->language;
 	}
 	
 	/**
 	 * Returns all available languages
 	 * @return	array	Languages
 	 */
 	public function listLanguages()
 	{
 		global $_paths;
 		
 		$dir = dirname($_paths->getLanguageFile($this->language));
 		$array = array();
 		
 		$HANDLE = opendir($dir);
 		
 		while( ($e = readdir($HANDLE)) != NULL )
 		{
 			// Skip special files
 			if( $e == "." || $e == ".." ) continue;
 			
 			$matches = array();
 			
 			// Extract language code
 			if( preg_match("/^(.*).lang.php$/","$e",$matches) > 0 )
	 			array_push($array, $matches[1]);
 		}
 		
 		return $array;
 	}
 }
?>